<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <el-card class="mt10" shadow="never" :body-style="{ padding: '5px 10px' }">
      <div slot="header" class="f16">
        <span style="font-weight: 600">订单生产动态</span>
      </div>
      <Table
        :tableHeader="tableHeader"
        :tableData="tableData"
        class="mt5"
        @isLoading="isLoading"
        @confirmChange="inputChange"
        @currentRow="currentRow"
      />
      <PageModule :PageData="{ total }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
    </el-card>

    <el-dialog v-model="dialog" title="标注结束的样品订单" width="40%">
      <Table :tableHeader="tableHeader2" :tableData="tableData2" class="mt5" @isLoading="isLoading2" @currentRow="currentRow2" />
      <el-button type="primary" size="default" @click="conceded">取消标注</el-button>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { defineComponent, onMounted, ref, watch } from 'vue'
import fileRequest from '@/utils/fileRequest'
import LoginWork from '@/utils/LoginWork'
import { ElMessage } from 'element-plus'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'ProductionPerformanceL'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import Find from '@/components/Find/index.vue'
import { selectList } from '@/plugins/mixin'
import service from '@/utils/request'
import { debounce } from 'lodash'
import Api from '@/api/GeneralManager/inquire/OrderSummary'

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入指令',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '品牌',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '季节',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '楦头',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '大底厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'date',
    dateLabel: '客户交期',
    dateValue: ''
  },

  {
    type: 'date',
    dateLabel: '成型计划日期',
    dateValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入工厂货号',
    inputValue: ''
  },
  {
    type: 'input',
    inputLabel: '核算日期',
    inputDisabled: true,
    inputValue: '624'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  },
  {
    type: 'button',
    checkboxLabel: '更新生产动态表'
  },
  {
    type: 'button',
    checkboxLabel: '标注指令已结束'
  },
  {
    type: 'button',
    checkboxLabel: '查询标注结束指令'
  }
]) as any
const tableHeader = ref([
  {
    change: true,
    fixed: true,
    label: '订单号',
    prop: 'lotNo',
    width: '150px'
  },
  {
    change: true,
    fixed: true,
    label: '品牌',
    prop: 'brand',
    width: '76px'
  },
  {
    change: true,
    fixed: true,
    label: '工厂货号',
    prop: 'style',
    width: '150px'
  },
  {
    change: true,
    fixed: true,
    label: '颜色',
    prop: 'color',
    width: '150px'
  },
  {
    change: true,
    fixed: true,
    label: '季节',
    prop: 'seasonType',
    width: '68px'
  },
  {
    change: true,
    fixed: true,
    label: '楦头',
    prop: 'lastNo',
    width: '130px'
  },
  {
    change: true,
    fixed: true,
    label: '大底厂商',
    prop: 'moldNo',
    width: '130px'
  },
  {
    change: true,
    fixed: true,
    label: '号码',
    prop: 'sizeNo',
    width: '68px'
  },
  {
    change: true,
    fixed: true,
    label: '数量',
    prop: 'orderQty',
    width: '68px'
  },
  {
    change: true,
    fixed: true,
    label: '下单日期',
    prop: 'orderDate',
    width: '68px'
  },
  {
    change: true,
    fixed: true,
    label: '客户交期',
    prop: 'deliveryDate',
    width: '68px'
  },

  {
    change: true,
    label: '版师',
    prop: 'samplePerson',
    width: '68px'
  },
  {
    change: true,
    label: '备料员',
    prop: 'mtlPerson',
    width: '68px'
  },
  {
    change: true,
    label: '开发员',
    prop: 'devPerson',
    width: '68px'
  },
  {
    Multistage: true,
    label: '纸板',
    children: [
      {
        type: 'dateSingle',
        label: '计划',
        prop: 'designPlan',
        width: '68px'
      },
      {
        change: true,
        label: '实际',
        prop: 'designAct',
        width: '68px'
      }
    ]
  },
  {
    Multistage: true,
    label: '材料',
    children: [
      {
        type: 'dateSingle',
        label: '计划',
        prop: 'mtlPlan',
        width: '68px'
      },
      {
        change: true,
        label: '实际',
        prop: 'mtlAct',
        width: '68px'
      }
    ]
  },
  {
    Multistage: true,
    label: '裁剪配套',
    children: [
      {
        label: '欠数',
        prop: 'cutShort',
        width: '68px'
      },
      {
        label: '计划',
        prop: 'cutPlan',
        width: '68px'
      },
      {
        change: true,
        label: '实际',
        prop: 'cutAct',
        width: '68px'
      }
    ]
  },
  {
    Multistage: true,
    label: '工艺加工',
    children: [
      {
        label: '欠数',
        prop: 'printShort',
        width: '68px'
      },
      {
        label: '计划',
        prop: 'printPlan',
        width: '68px'
      },
      {
        label: '实际',
        prop: 'printAct',
        width: '68px'
      }
    ]
  },
  {
    Multistage: true,
    label: '大底厂商',
    children: [
      {
        label: '欠数',
        prop: 'embShort',
        width: '68px'
      },
      {
        label: '计划',
        prop: 'embPlan',
        width: '68px'
      },
      {
        label: '实际',
        prop: 'embAct',
        width: '68px'
      }
    ]
  },
  {
    Multistage: true,
    label: '面衬',
    children: [
      {
        label: '欠数',
        prop: 'hfShort',
        width: '68px'
      },
      {
        label: '计划',
        prop: 'hfPlan',
        width: '68px'
      },
      {
        change: true,
        label: '实际',
        prop: 'hfAct',
        width: '68px'
      }
    ]
  },
  {
    Multistage: true,
    label: '针车',
    children: [
      {
        label: '欠数',
        prop: 'stitchShort',
        width: '68px'
      },
      {
        label: '计划',
        prop: 'stitchPlan',
        width: '68px'
      },
      {
        label: '实际',
        prop: 'stitchAct',
        width: '68px'
      }
    ]
  },
  {
    Multistage: true,
    label: '成型',
    children: [
      {
        label: '欠数',
        prop: 'lastShort',
        width: '68px'
      },
      {
        label: '计划',
        prop: 'lastPlan',
        width: '68px'
      },
      {
        change: true,
        label: '实际',
        prop: 'lastAct',
        width: '68px'
      }
    ]
  }
])
const tableHeader2 = ref([
  {
    label: '订单',
    prop: 'order'
  }
])
const tableData2 = ref([])
const isLoading2 = ref(true)

// 表一模块
// '置空'按钮
const dialog = ref(false)
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
    getTableList()
  }
  if (content === '导出') {
    const res = (await fileRequest.post(`/pcOrder/downloadPcProductDt`, {
      deliveryEndDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '客户交期'
      )?.dateValue?.[1],
      deliveryStartDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '客户交期'
      )?.dateValue?.[0],
      lastNo: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '楦头')
        ?.selectValue,
      lotNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令'
      )?.inputValue,
      moldNo: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '大底厂商')
        ?.selectValue,
      prodPlanEndDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '成型计划日期'
      )?.dateValue?.[1],
      prodPlanStartDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '成型计划日期'
      )?.dateValue?.[0],
      season: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '季节')
        ?.selectValue,
      style: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入工厂货号'
      )?.inputValue,
      brand: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌')
        ?.selectValue,
      pageNum: currentPage.value,
      pageSize: pageSize.value
    })) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
  if (content === '更新生产动态表') {
    const res = (await LoginWork.put('/pcOrder/updatePcOrderTable')) as any
    if (res.code === 200) {
      JMessage.success('更新生产动态表成功')
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '标注指令已结束') {
    const res = (await LoginWork.put(`/pcOrder/dt/pcProductionDtMark?lotNo=${currentTableRow.value.lotNo}`)) as any
    if (res.code === 200) {
      JMessage.success('成功')
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '查询标注结束指令') {
    dialog.value = true
    getTableList2()
  }
}
const tableData = ref([])
const isLoading = ref(true)

// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}

// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/pcOrder/dt/pcProductionDt', {
    deliveryEndDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '客户交期'
    )?.dateValue?.[1],
    deliveryStartDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '客户交期'
    )?.dateValue?.[0],
    lastNo: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '楦头')
      ?.selectValue,
    lotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令'
    )?.inputValue,
    moldNo: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '大底厂商')
      ?.selectValue,
    prodPlanEndDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '成型计划日期'
    )?.dateValue?.[1],
    prodPlanStartDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '成型计划日期'
    )?.dateValue?.[0],
    season: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '季节')
      ?.selectValue,
    style: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入工厂货号'
    )?.inputValue,
    brand: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌')
      ?.selectValue,
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.list.map((item: any) => {
    item.change = 'Y'
    return item
  })
  total.value = tableDataList.total
  currentRow()
}, 500)
const getTableList2 = async () => {
  const res = (await LoginWork.get(`/pcOrder/dt/pcProductionDtMarkGet`)) as any
  if (res.code === 200) {
    tableData2.value = res.data.map((item: any) => {
      return {
        order: item
      }
    })
  } else {
    JMessage.error(res.message)
  }
}
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentTableRow2 = ref({}) as any
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  currentTableRow.value = row
}
const currentRow2 = async (row: any = tableData2.value[0]) => {
  currentTableRow2.value = row
}
const inputChange = async () => {
  console.log(currentTableRow.value)
  const res = (await LoginWork.put('/pcOrder/dt/updatePcProductionDtPlanDate', currentTableRow.value)) as any
  if (res.code === 200) {
    JMessage.success('修改成功')
  } else {
    JMessage.error(res.message)
    getTableList()
  }
}
onMounted(() => {
  getTableList()
  getSelectList()
})
const getSelectList = async () => {
  // selectList(formList, () => service.get<any, any>('/style/getCondition'))
  let res = await service.get<any, any>('/style/getCondition')
  // 底膜
  // res.moldNo.forEach((item: any) => {
  //   if (item === null || item === '') return
  //   formList.value.find((item: any) => item.type === 'select' && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '底模').selectOptions.push({
  //     optionValue: item,
  //     optionLabel: item
  //   })
  // })
  // lastNo 楦头
  res.lastNo.forEach((item: any) => {
    if (item === null || item === '') return
    formList.value
      .find((item: any) => item.type === 'select' && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '楦头')
      .selectOptions.push({
        optionValue: item,
        optionLabel: item
      })
  })

  // custom  品牌
  res.custom.forEach((item: any) => {
    if (item === null || item === '') return
    formList.value
      .find((item: any) => item.type === 'select' && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌')
      .selectOptions.push({
        optionValue: item,
        optionLabel: item
      })
  })

  // cutNo 冲刀
  // res.cutNo.forEach((item: any) => {
  //   if (item === null || item === '') return
  //   formList.value.find((item: any) => item.type === 'select' && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '冲刀').selectOptions.push({
  //     optionValue: item,
  //     optionLabel: item
  //   })
  // })
  // season 季度
  res.season.forEach((item: any) => {
    if (item === null || item === '') return
    formList.value
      .find((item: any) => item.type === 'select' && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '季节')
      .selectOptions.push({
        optionValue: item,
        optionLabel: item
      })
  })
  // soleVendor 大底厂商
  res.soleVendor.forEach((item: any) => {
    if (item === null || item === '') return
    formList.value
      .find((item: any) => item.type === 'select' && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '大底厂商')
      .selectOptions.push({
        optionValue: item,
        optionLabel: item
      })
  })
}
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)

// 取消标注按钮
const conceded = async () => {
  const res = (await LoginWork.post(`/pcOrder/dt/pcProductionDtCancelMarkOk?lotNo=${currentTableRow2.value.order}`)) as any
  if (res.code === 200) {
    JMessage.success('取消成功')
    getTableList()
    dialog.value = false
  } else {
    JMessage.error(res.message)
  }
}
</script>

<style lang="less" scoped></style>
